package com.atguigu.ggkt.vod.service.impl;

import com.atguigu.ggkt.model.vod.Course;
import com.atguigu.ggkt.model.vod.CourseDescription;
import com.atguigu.ggkt.model.vod.Subject;
import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.vo.vod.*;
import com.atguigu.ggkt.vod.mapper.CourseMapper;
import com.atguigu.ggkt.vod.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-03-31
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;

    //课程列表
    @Override
    public Map<String, Object> findPage(Page<Course> coursePage, CourseQueryVo courseQueryVo) {
        //获取条件值
        String title = courseQueryVo.getTitle(); //名称
        Long subjectId = courseQueryVo.getSubjectId(); //二级分类
        Long subjectParentId = courseQueryVo.getSubjectParentId(); //一级分类
        Long teacherId = courseQueryVo.getTeacherId(); //讲师
        //根据条件封装语句
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(title != null){
            lambdaQueryWrapper.like(Course::getTitle,title);
        }
        if(subjectId != null){
            lambdaQueryWrapper.eq(Course::getSubjectId,subjectId);
        }
        if(subjectParentId != null){
            lambdaQueryWrapper.eq(Course::getSubjectParentId,subjectParentId);
        }
        if(teacherId != null){
            lambdaQueryWrapper.eq(Course::getTeacherId,teacherId);
        }
        //调用方法查询
        Page<Course> pages = baseMapper.selectPage(coursePage, lambdaQueryWrapper);
        //拿出pages中的数据封装到map集合中
        long totalCount = pages.getTotal(); //总记录数
        long totalPage = pages.getPages(); //总页数
        List<Course> records = pages.getRecords(); //每页数据集合
        //使用stream流遍历封装讲师和分类的名称
        records.stream().forEach(item ->{
            this.getTeacherOrSubjectName(item);
        });
        //封装返回数据    
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",records);
        return map;
    }

    //根据id查询课程
    @Override
    public Map<String, Object> getInfoById(Long id) {
        //更新流量
        Course course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);

        Map<String, Object> map = new HashMap<>();
        CourseVo courseVo = baseMapper.selectCourseVoById(id);
        List<ChapterVo> chapterVoList = chapterService.getNestedTreeList(id);
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        Teacher teacher = teacherService.getById(course.getTeacherId());

        //TODO后续完善
        Boolean isBuy = false;

        map.put("courseVo", courseVo);
        map.put("chapterVoList", chapterVoList);
        map.put("description", null != courseDescription ?
                courseDescription.getDescription() : "");
        map.put("teacher", teacher);
        map.put("isBuy", isBuy);//是否购买
        return map;
    }

    //新增课程
    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {
        //先把课程信息添加到课程表course中
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.insert(course);
        //再把课程描述添加到课程信息描述表course_description中
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setCourseId(course.getId());
        courseDescriptionService.save(courseDescription);
        //返回课程id
        return course.getId();
    }

    //根据id查询课程信息
    @Override
    public CourseFormVo getCourseFormVoById(Long id) {
        //course表中基本信息封装到courseForm
        Course course = baseMapper.selectById(id);
        if(course == null){
            return null;
        }
        //course_description中描述信息封装到courseForm
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        //创建courseForm对象
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        if(courseDescription != null){
            courseFormVo.setDescription(courseDescription.getDescription());
        }
        return courseFormVo;
    }

    //根据id修改课程信息
    @Override
    public void updateCourseById(CourseFormVo courseFormVo) {
        //修改课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);
        //修改课程详情信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionService.updateById(courseDescription);
    }

    //根据id获取课程发布信息
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(id);
        return coursePublishVo;
    }

    //根据id发布课程
    @Override
    public void publishCourseById(Long id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1);
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
    }

    //删除课程
    @Override
    public void removeCourseById(Long id) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);
        //根据课程id删除描述
        courseDescriptionService.removeById(id);
        //根据课程id删除课程
        baseMapper.deleteById(id);
    }

    private Course getTeacherOrSubjectName(Course course) {
        //查询讲师名称
        //根据course中的Teacher_id到Teacher表中查到讲师名称
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher != null){
            course.getParam().put("teacherName",teacher.getName());
        }
        //查询分类名称
        Subject subjectOne = subjectService.getById(course.getSubjectParentId());
        //一级分类
        if(subjectOne != null) {
            course.getParam().put("subjectParentTitle",subjectOne.getTitle());
        }
        Subject subjectTwo = subjectService.getById(course.getSubjectId());
        //二级分类
        if(subjectTwo != null) {
            course.getParam().put("subjectTitle",subjectTwo.getTitle());
        }
        return course;
    }
}
